<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>【一行代码可以实现的事】数组篇 | Vixcity</title><meta name="keywords" content="代码片段,JavaScript,一行代码"><meta name="author" content="Vixcity"><meta name="copyright" content="Vixcity"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="【一行代码可以实现的事】数组篇将值转换为数组12345const castArray &#x3D; value &#x3D;&gt; Array.isArray(value) ? value : [value];&#x2F;&#x2F; ExamplecastArray(1);               &#x2F;&#x2F; [1]castArray([1, 2, 3]);       &#x2F;&#x2F; [1, 2, 3]  检查数组是否为空123456&#x2F;&#x2F; &#96;">
<meta property="og:type" content="article">
<meta property="og:title" content="【一行代码可以实现的事】数组篇">
<meta property="og:url" content="http://vixcity.gitee.io/my-gitee-blog/2021/09/23/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91%E6%95%B0%E7%BB%84%E7%AF%87/index.html">
<meta property="og:site_name" content="Vixcity">
<meta property="og:description" content="【一行代码可以实现的事】数组篇将值转换为数组12345const castArray &#x3D; value &#x3D;&gt; Array.isArray(value) ? value : [value];&#x2F;&#x2F; ExamplecastArray(1);               &#x2F;&#x2F; [1]castArray([1, 2, 3]);       &#x2F;&#x2F; [1, 2, 3]  检查数组是否为空123456&#x2F;&#x2F; &#96;">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://vixcity.gitee.io/my-gitee-blog/img/JS.jpg">
<meta property="article:published_time" content="2021-09-22T16:00:00.000Z">
<meta property="article:modified_time" content="2022-01-04T03:04:47.333Z">
<meta property="article:author" content="Vixcity">
<meta property="article:tag" content="代码片段">
<meta property="article:tag" content="JavaScript">
<meta property="article:tag" content="一行代码">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://vixcity.gitee.io/my-gitee-blog/img/JS.jpg"><link rel="shortcut icon" href="/my-gitee-blog/img/favicon.ico"><link rel="canonical" href="http://vixcity.gitee.io/my-gitee-blog/2021/09/23/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91%E6%95%B0%E7%BB%84%E7%AF%87/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//fonts.googleapis.com" crossorigin=""/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/my-gitee-blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Titillium+Web&amp;display=swap" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/my-gitee-blog/',
  algolia: undefined,
  localSearch: {"path":"/my-gitee-blog/search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":1,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: '/my-gitee-blog/',
      css: '/my-gitee-blog/'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '【一行代码可以实现的事】数组篇',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: false,
  postUpdate: '2022-01-04 11:04:47'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/my-gitee-blog/css/mycss.css"><link rel="stylesheet" href="/my-gitee-blog/css/newStyle.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome-animation@1.1.1/css/font-awesome-animation.css"><link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-card-history/baiduhistory/css/main.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.css"><link rel="stylesheet" href="https://unpkg.zhimg.com/hexo-butterfly-wowjs/lib/animate.min.css" media="print" onload="this.media='screen'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/my-gitee-blog/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/my-gitee-blog/archives/"><div class="headline">文章</div><div class="length-num">55</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/my-gitee-blog/tags/"><div class="headline">标签</div><div class="length-num">50</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/my-gitee-blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/my-gitee-blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/PicBed"><i class="fa-fw fas fa-image"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/my-gitee-blog/img/JS.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/my-gitee-blog/">Vixcity</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/my-gitee-blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/PicBed"><i class="fa-fw fas fa-image"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/my-gitee-blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">【一行代码可以实现的事】数组篇</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-09-22T16:00:00.000Z" title="发表于 2021-09-23 00:00:00">2021-09-23</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-01-04T03:04:47.333Z" title="更新于 2022-01-04 11:04:47">2022-01-04</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/my-gitee-blog/categories/%E5%A4%A7%E5%89%8D%E7%AB%AF/">大前端</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/my-gitee-blog/categories/%E5%A4%A7%E5%89%8D%E7%AB%AF/%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81/">一行代码</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">3k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>16分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="【一行代码可以实现的事】数组篇"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="【一行代码可以实现的事】数组篇"><a href="#【一行代码可以实现的事】数组篇" class="headerlink" title="【一行代码可以实现的事】数组篇"></a>【一行代码可以实现的事】数组篇</h2><h2 id="将值转换为数组"><a href="#将值转换为数组" class="headerlink" title="将值转换为数组"></a>将值转换为数组</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> castArray = <span class="function"><span class="params">value</span> =&gt;</span> <span class="built_in">Array</span>.isArray(value) ? value : [value];</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">castArray(<span class="number">1</span>);               <span class="comment">// [1]</span></span><br><span class="line">castArray([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]);       <span class="comment">// [1, 2, 3]</span></span><br></pre></td></tr></table></figure>

<h2 id="检查数组是否为空"><a href="#检查数组是否为空" class="headerlink" title="检查数组是否为空"></a>检查数组是否为空</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// `arr` is an array</span></span><br><span class="line"><span class="keyword">const</span> isEmpty = <span class="function"><span class="params">arr</span> =&gt;</span> !<span class="built_in">Array</span>.isArray(arr) || arr.length === <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">isEmpty([]);            <span class="comment">// true</span></span><br><span class="line">isEmpty([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]);     <span class="comment">// false</span></span><br></pre></td></tr></table></figure>

<h2 id="克隆一个数组"><a href="#克隆一个数组" class="headerlink" title="克隆一个数组"></a>克隆一个数组</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// `arr` is an array</span></span><br><span class="line"><span class="keyword">const</span> clone = <span class="function"><span class="params">arr</span> =&gt;</span> arr.slice(<span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> clone = <span class="function"><span class="params">arr</span> =&gt;</span> [...arr];</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> clone = <span class="function"><span class="params">arr</span> =&gt;</span> <span class="built_in">Array</span>.from(arr);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> clone = <span class="function"><span class="params">arr</span> =&gt;</span> arr.map(<span class="function"><span class="params">x</span> =&gt;</span> x);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> clone = <span class="function"><span class="params">arr</span> =&gt;</span> <span class="built_in">JSON</span>.parse(<span class="built_in">JSON</span>.stringify(arr));</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> clone = <span class="function"><span class="params">arr</span> =&gt;</span> arr.concat([]);</span><br></pre></td></tr></table></figure>

<h2 id="比较两个数组"><a href="#比较两个数组" class="headerlink" title="比较两个数组"></a>比较两个数组</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// `a` and `b` are arrays</span></span><br><span class="line"><span class="keyword">const</span> isEqual = <span class="function">(<span class="params">a, b</span>) =&gt;</span> <span class="built_in">JSON</span>.stringify(a) === <span class="built_in">JSON</span>.stringify(b);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> isEqual = <span class="function">(<span class="params">a, b</span>) =&gt;</span> a.length === b.length &amp;&amp; a.every(<span class="function">(<span class="params">v, i</span>) =&gt;</span> v === b[i]);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">isEqual([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]);      <span class="comment">// true</span></span><br><span class="line">isEqual([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], [<span class="number">1</span>, <span class="string">&#x27;2&#x27;</span>, <span class="number">3</span>]);    <span class="comment">// false</span></span><br></pre></td></tr></table></figure>

<h2 id="不管顺序如何比较两个数组"><a href="#不管顺序如何比较两个数组" class="headerlink" title="不管顺序如何比较两个数组"></a>不管顺序如何比较两个数组</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// `a` and `b` are arrays</span></span><br><span class="line"><span class="keyword">const</span> isEqual = <span class="function">(<span class="params">a, b</span>) =&gt;</span> <span class="built_in">JSON</span>.stringify(a.sort()) === <span class="built_in">JSON</span>.stringify(b.sort());</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">isEqual([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]);      <span class="comment">// true</span></span><br><span class="line">isEqual([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], [<span class="number">1</span>, <span class="number">3</span>, <span class="number">2</span>]);      <span class="comment">// true</span></span><br><span class="line">isEqual([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], [<span class="number">1</span>, <span class="string">&#x27;2&#x27;</span>, <span class="number">3</span>]);    <span class="comment">// false</span></span><br></pre></td></tr></table></figure>

<h2 id="将对象数组转换为单个对象"><a href="#将对象数组转换为单个对象" class="headerlink" title="将对象数组转换为单个对象"></a>将对象数组转换为单个对象</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> toObject = <span class="function">(<span class="params">arr, key</span>) =&gt;</span> arr.reduce(<span class="function">(<span class="params">a, b</span>) =&gt;</span> (&#123; ...a, [b[key]]: b &#125;), &#123;&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">toObject(</span><br><span class="line"> [</span><br><span class="line">  &#123; <span class="attr">id</span>: <span class="string">&#x27;1&#x27;</span>, <span class="attr">name</span>: <span class="string">&#x27;Alpha&#x27;</span>, <span class="attr">gender</span>: <span class="string">&#x27;Male&#x27;</span> &#125;,</span><br><span class="line">  &#123; <span class="attr">id</span>: <span class="string">&#x27;2&#x27;</span>, <span class="attr">name</span>: <span class="string">&#x27;Bravo&#x27;</span>, <span class="attr">gender</span>: <span class="string">&#x27;Male&#x27;</span> &#125;,</span><br><span class="line">  &#123; <span class="attr">id</span>: <span class="string">&#x27;3&#x27;</span>, <span class="attr">name</span>: <span class="string">&#x27;Charlie&#x27;</span>, <span class="attr">gender</span>: <span class="string">&#x27;Female&#x27;</span> &#125;,</span><br><span class="line"> ],</span><br><span class="line"> <span class="string">&#x27;id&#x27;</span></span><br><span class="line">);</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">&#123;</span></span><br><span class="line"><span class="comment"> &#x27;1&#x27;: &#123; id: &#x27;1&#x27;, name: &#x27;Alpha&#x27;, gender: &#x27;Male&#x27; &#125;,</span></span><br><span class="line"><span class="comment"> &#x27;2&#x27;: &#123; id: &#x27;2&#x27;, name: &#x27;Bravo&#x27;, gender: &#x27;Male&#x27; &#125;,</span></span><br><span class="line"><span class="comment"> &#x27;3&#x27;: &#123; id: &#x27;3&#x27;, name: &#x27;Charlie&#x27;, gender: &#x27;Female&#x27; &#125;,</span></span><br><span class="line"><span class="comment">&#125;</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

<h2 id="将字符串数组转换为数字"><a href="#将字符串数组转换为数字" class="headerlink" title="将字符串数组转换为数字"></a>将字符串数组转换为数字</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> toNumbers = <span class="function"><span class="params">arr</span> =&gt;</span> arr.map(<span class="built_in">Number</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> toNumbers = <span class="function"><span class="params">arr</span> =&gt;</span> arr.map(<span class="function"><span class="params">x</span> =&gt;</span> +x);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">toNumbers([<span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;4&#x27;</span>]);     <span class="comment">// [2, 3, 4]</span></span><br></pre></td></tr></table></figure>

<h2 id="按对象数组的属性计数"><a href="#按对象数组的属性计数" class="headerlink" title="按对象数组的属性计数"></a>按对象数组的属性计数</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> countBy = <span class="function">(<span class="params">arr, prop</span>) =&gt;</span> arr.reduce(<span class="function">(<span class="params">prev, curr</span>) =&gt;</span> (prev[curr[prop]] = ++prev[curr[prop]] || <span class="number">1</span>, prev), &#123;&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">countBy([</span><br><span class="line"> &#123; <span class="attr">branch</span>: <span class="string">&#x27;audi&#x27;</span>, <span class="attr">model</span>: <span class="string">&#x27;q8&#x27;</span>, <span class="attr">year</span>: <span class="string">&#x27;2019&#x27;</span> &#125;,</span><br><span class="line"> &#123; <span class="attr">branch</span>: <span class="string">&#x27;audi&#x27;</span>, <span class="attr">model</span>: <span class="string">&#x27;rs7&#x27;</span>, <span class="attr">year</span>: <span class="string">&#x27;2020&#x27;</span> &#125;,</span><br><span class="line"> &#123; <span class="attr">branch</span>: <span class="string">&#x27;ford&#x27;</span>, <span class="attr">model</span>: <span class="string">&#x27;mustang&#x27;</span>, <span class="attr">year</span>: <span class="string">&#x27;2019&#x27;</span> &#125;,</span><br><span class="line"> &#123; <span class="attr">branch</span>: <span class="string">&#x27;ford&#x27;</span>, <span class="attr">model</span>: <span class="string">&#x27;explorer&#x27;</span>, <span class="attr">year</span>: <span class="string">&#x27;2020&#x27;</span> &#125;,</span><br><span class="line"> &#123; <span class="attr">branch</span>: <span class="string">&#x27;bmw&#x27;</span>, <span class="attr">model</span>: <span class="string">&#x27;x7&#x27;</span>, <span class="attr">year</span>: <span class="string">&#x27;2020&#x27;</span> &#125;,</span><br><span class="line">], <span class="string">&#x27;branch&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// &#123; &#x27;audi&#x27;: 2, &#x27;ford&#x27;: 2, &#x27;bmw&#x27;: 1 &#125;</span></span><br></pre></td></tr></table></figure>

<h2 id="计算数组中某个值的出现次数"><a href="#计算数组中某个值的出现次数" class="headerlink" title="计算数组中某个值的出现次数"></a>计算数组中某个值的出现次数</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> countOccurrences = <span class="function">(<span class="params">arr, val</span>) =&gt;</span> arr.reduce(<span class="function">(<span class="params">a, v</span>) =&gt;</span> (v === val ? a + <span class="number">1</span> : a), <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> countOccurrences = <span class="function">(<span class="params">arr, val</span>) =&gt;</span> arr.filter(<span class="function"><span class="params">item</span> =&gt;</span> item === val).length;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">countOccurrences([<span class="number">2</span>, <span class="number">1</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">3</span>], <span class="number">2</span>);                <span class="comment">// 2</span></span><br><span class="line">countOccurrences([<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>], <span class="string">&#x27;a&#x27;</span>);  <span class="comment">// 3</span></span><br></pre></td></tr></table></figure>

<h2 id="计算数组元素的出现次数"><a href="#计算数组元素的出现次数" class="headerlink" title="计算数组元素的出现次数"></a>计算数组元素的出现次数</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> countOccurrences = <span class="function"><span class="params">arr</span> =&gt;</span> arr.reduce(<span class="function">(<span class="params">prev, curr</span>) =&gt;</span> (prev[curr] = ++prev[curr] || <span class="number">1</span>, prev), &#123;&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">countOccurrences([<span class="number">2</span>, <span class="number">1</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">3</span>]);               <span class="comment">// &#123; &#x27;1&#x27;: 1, &#x27;2&#x27;: 2, &#x27;3&#x27;: 3 &#125;</span></span><br><span class="line">countOccurrences([<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>]);   <span class="comment">// &#123; &#x27;a&#x27;: 3, &#x27;b&#x27;: 2, &#x27;c&#x27;: 1 &#125;</span></span><br></pre></td></tr></table></figure>

<h2 id="创建一个累积和的数组"><a href="#创建一个累积和的数组" class="headerlink" title="创建一个累积和的数组"></a>创建一个累积和的数组</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> accumulate = <span class="function"><span class="params">arr</span> =&gt;</span> arr.map((<span class="function"><span class="params">sum</span> =&gt;</span> <span class="function"><span class="params">value</span> =&gt;</span> sum += value)(<span class="number">0</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> accumulate = <span class="function"><span class="params">arr</span> =&gt;</span> arr.reduce(<span class="function">(<span class="params">a, b, i</span>) =&gt;</span> i === <span class="number">0</span> ? [b] : [...a, b + a[i - <span class="number">1</span>]], []);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> accumulate = <span class="function"><span class="params">arr</span> =&gt;</span> arr.reduce(<span class="function">(<span class="params">a, b, i</span>) =&gt;</span> i === <span class="number">0</span> ? [b] : [...a, b + a[i - <span class="number">1</span>]], <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">accumulate([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]);   <span class="comment">// [1, 3, 6, 10]</span></span><br><span class="line"><span class="comment">// 1             = 1</span></span><br><span class="line"><span class="comment">// 1 + 2         = 3</span></span><br><span class="line"><span class="comment">// 1 + 2 + 3     = 6</span></span><br><span class="line"><span class="comment">// 1 + 2 + 3 + 4 = 10</span></span><br></pre></td></tr></table></figure>

<h2 id="创建给定范围内的数字数组"><a href="#创建给定范围内的数字数组" class="headerlink" title="创建给定范围内的数字数组"></a>创建给定范围内的数字数组</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> range = <span class="function">(<span class="params">min, max</span>) =&gt;</span> [...Array(max - min + <span class="number">1</span>).keys()].map(<span class="function"><span class="params">i</span> =&gt;</span> i + min);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> range = <span class="function">(<span class="params">min, max</span>) =&gt;</span> <span class="built_in">Array</span>(max - min + <span class="number">1</span>).fill(<span class="number">0</span>).map(<span class="function">(<span class="params">_, i</span>) =&gt;</span> min + i);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> range = <span class="function">(<span class="params">min, max</span>) =&gt;</span> <span class="built_in">Array</span>.from(&#123; <span class="attr">length</span>: max - min + <span class="number">1</span> &#125;, <span class="function">(<span class="params">_, i</span>) =&gt;</span> min + i);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line"> range(<span class="number">5</span>, <span class="number">10</span>);   <span class="comment">// [5, 6, 7, 8, 9, 10]</span></span><br></pre></td></tr></table></figure>

<h2 id="创建笛卡尔积"><a href="#创建笛卡尔积" class="headerlink" title="创建笛卡尔积"></a>创建笛卡尔积</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> cartesian = <span class="function">(<span class="params">...sets</span>) =&gt;</span> sets.reduce(<span class="function">(<span class="params">acc, set</span>) =&gt;</span> acc.flatMap(<span class="function">(<span class="params">x</span>) =&gt;</span> set.map(<span class="function">(<span class="params">y</span>) =&gt;</span> [...x, y])), [[]]);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">cartesian([<span class="number">1</span>, <span class="number">2</span>], [<span class="number">3</span>, <span class="number">4</span>]);   <span class="comment">// [ [1, 3], [1, 4], [2, 3], [2, 4] ]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">       3       4</span></span><br><span class="line"><span class="comment">    ---------------</span></span><br><span class="line"><span class="comment"> 1 | [1, 3]  [1, 4]</span></span><br><span class="line"><span class="comment">   |</span></span><br><span class="line"><span class="comment"> 2 | [2, 3]  [2, 4]</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

<h2 id="清空数组"><a href="#清空数组" class="headerlink" title="清空数组"></a>清空数组</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> empty = <span class="function"><span class="params">arr</span> =&gt;</span> arr.length = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line">arr = [];</span><br></pre></td></tr></table></figure>

<h2 id="从数组中找到最接近的数字"><a href="#从数组中找到最接近的数字" class="headerlink" title="从数组中找到最接近的数字"></a>从数组中找到最接近的数字</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Find the number from `arr` which is closest to `n`</span></span><br><span class="line"><span class="keyword">const</span> closest = <span class="function">(<span class="params">arr, n</span>) =&gt;</span> arr.reduce(<span class="function">(<span class="params">prev, curr</span>) =&gt;</span> <span class="built_in">Math</span>.abs(curr - n) &lt; <span class="built_in">Math</span>.abs(prev - n) ? curr : prev);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> closest = <span class="function">(<span class="params">arr, n</span>) =&gt;</span> arr.sort(<span class="function">(<span class="params">a, b</span>) =&gt;</span> <span class="built_in">Math</span>.abs(a - n) - <span class="built_in">Math</span>.abs(b - n))[<span class="number">0</span>];</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">closest([<span class="number">29</span>, <span class="number">87</span>, <span class="number">8</span>, <span class="number">78</span>, <span class="number">97</span>, <span class="number">20</span>, <span class="number">75</span>, <span class="number">33</span>, <span class="number">24</span>, <span class="number">17</span>], <span class="number">50</span>);   <span class="comment">// 33</span></span><br></pre></td></tr></table></figure>

<h2 id="查找数组最后一个匹配项的索引"><a href="#查找数组最后一个匹配项的索引" class="headerlink" title="查找数组最后一个匹配项的索引"></a>查找数组最后一个匹配项的索引</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> lastIndex = <span class="function">(<span class="params">arr, predicate</span>) =&gt;</span> arr.reduce(<span class="function">(<span class="params">prev, curr, index</span>) =&gt;</span> predicate(curr) ? index : prev, -<span class="number">1</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> lastIndex = <span class="function">(<span class="params">arr, predicate</span>) =&gt;</span> arr.map(<span class="function"><span class="params">item</span> =&gt;</span> predicate(item)).lastIndexOf(<span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">lastIndex([<span class="number">1</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">7</span>, <span class="number">9</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">6</span>, <span class="number">8</span>], <span class="function"><span class="params">i</span> =&gt;</span> i % <span class="number">2</span> === <span class="number">1</span>);   <span class="comment">// 4</span></span><br><span class="line">lastIndex([<span class="number">1</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">7</span>, <span class="number">9</span>, <span class="number">8</span>, <span class="number">6</span>, <span class="number">4</span>, <span class="number">2</span>], <span class="function"><span class="params">i</span> =&gt;</span> i &gt; <span class="number">6</span>);         <span class="comment">// 5</span></span><br></pre></td></tr></table></figure>

<h2 id="查找数组最大项的索引"><a href="#查找数组最大项的索引" class="headerlink" title="查找数组最大项的索引"></a>查找数组最大项的索引</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> indexOfMax = <span class="function"><span class="params">arr</span> =&gt;</span> arr.reduce(<span class="function">(<span class="params">prev, curr, i, a</span>) =&gt;</span> curr &gt; a[prev] ? i : prev, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">indexOfMax([<span class="number">1</span>, <span class="number">3</span>, <span class="number">9</span>, <span class="number">7</span>, <span class="number">5</span>]);        <span class="comment">// 2</span></span><br><span class="line">indexOfMax([<span class="number">1</span>, <span class="number">3</span>, <span class="number">7</span>, <span class="number">7</span>, <span class="number">5</span>]);        <span class="comment">// 2</span></span><br></pre></td></tr></table></figure>

<h2 id="查找数组中最小项的索引"><a href="#查找数组中最小项的索引" class="headerlink" title="查找数组中最小项的索引"></a>查找数组中最小项的索引</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> indexOfMin = <span class="function"><span class="params">arr</span> =&gt;</span> arr.reduce(<span class="function">(<span class="params">prev, curr, i, a</span>) =&gt;</span> curr &lt; a[prev] ? i : prev, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">indexOfMin([<span class="number">6</span>, <span class="number">4</span>, <span class="number">8</span>, <span class="number">2</span>, <span class="number">10</span>]);       <span class="comment">// 3</span></span><br><span class="line">indexOfMin([<span class="number">6</span>, <span class="number">4</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">10</span>]);       <span class="comment">// 2</span></span><br></pre></td></tr></table></figure>

<h2 id="查找数组中最长字符串的长度"><a href="#查找数组中最长字符串的长度" class="headerlink" title="查找数组中最长字符串的长度"></a>查找数组中最长字符串的长度</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> findLongest = <span class="function"><span class="params">words</span> =&gt;</span> <span class="built_in">Math</span>.max(...(words.map(<span class="function"><span class="params">el</span> =&gt;</span> el.length)));</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">findLongest([<span class="string">&#x27;always&#x27;</span>,<span class="string">&#x27;look&#x27;</span>,<span class="string">&#x27;on&#x27;</span>,<span class="string">&#x27;the&#x27;</span>,<span class="string">&#x27;bright&#x27;</span>,<span class="string">&#x27;side&#x27;</span>,<span class="string">&#x27;of&#x27;</span>,<span class="string">&#x27;life&#x27;</span>]);  <span class="comment">// 6</span></span><br></pre></td></tr></table></figure>

<h2 id="查找数组的最大项"><a href="#查找数组的最大项" class="headerlink" title="查找数组的最大项"></a>查找数组的最大项</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> max = <span class="function"><span class="params">arr</span> =&gt;</span> <span class="built_in">Math</span>.max(...arr);</span><br></pre></td></tr></table></figure>

<h2 id="通过给定的键查找数组的最大项"><a href="#通过给定的键查找数组的最大项" class="headerlink" title="通过给定的键查找数组的最大项"></a>通过给定的键查找数组的最大项</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> maxBy = <span class="function">(<span class="params">arr, key</span>) =&gt;</span> arr.reduce(<span class="function">(<span class="params">a, b</span>) =&gt;</span> a[key] &gt;= b[key] ? a : b, &#123;&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line"><span class="keyword">const</span> people = [</span><br><span class="line"> &#123; <span class="attr">name</span>: <span class="string">&#x27;Bar&#x27;</span>, <span class="attr">age</span>: <span class="number">24</span> &#125;,</span><br><span class="line"> &#123; <span class="attr">name</span>: <span class="string">&#x27;Baz&#x27;</span>, <span class="attr">age</span>: <span class="number">32</span> &#125;,</span><br><span class="line"> &#123; <span class="attr">name</span>: <span class="string">&#x27;Foo&#x27;</span>, <span class="attr">age</span>: <span class="number">42</span> &#125;,</span><br><span class="line"> &#123; <span class="attr">name</span>: <span class="string">&#x27;Fuzz&#x27;</span>, <span class="attr">age</span>: <span class="number">36</span> &#125;,</span><br><span class="line">];</span><br><span class="line">maxBy(people, <span class="string">&#x27;age&#x27;</span>);   <span class="comment">// &#123; name: &#x27;Foo&#x27;, age: 42 &#125;</span></span><br></pre></td></tr></table></figure>

<h2 id="查找数组的最小项"><a href="#查找数组的最小项" class="headerlink" title="查找数组的最小项"></a>查找数组的最小项</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> min = <span class="function"><span class="params">arr</span> =&gt;</span> <span class="built_in">Math</span>.min(...arr);</span><br></pre></td></tr></table></figure>

<h2 id="通过给定的键查找数组的最小项"><a href="#通过给定的键查找数组的最小项" class="headerlink" title="通过给定的键查找数组的最小项"></a>通过给定的键查找数组的最小项</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> minBy = <span class="function">(<span class="params">arr, key</span>) =&gt;</span> arr.reduce(<span class="function">(<span class="params">a, b</span>) =&gt;</span> a[key] &lt; b[key] ? a : b, &#123;&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line"><span class="keyword">const</span> people = [</span><br><span class="line"> &#123; <span class="attr">name</span>: <span class="string">&#x27;Bar&#x27;</span>, <span class="attr">age</span>: <span class="number">24</span> &#125;,</span><br><span class="line"> &#123; <span class="attr">name</span>: <span class="string">&#x27;Baz&#x27;</span>, <span class="attr">age</span>: <span class="number">32</span> &#125;,</span><br><span class="line"> &#123; <span class="attr">name</span>: <span class="string">&#x27;Foo&#x27;</span>, <span class="attr">age</span>: <span class="number">42</span> &#125;,</span><br><span class="line"> &#123; <span class="attr">name</span>: <span class="string">&#x27;Fuzz&#x27;</span>, <span class="attr">age</span>: <span class="number">36</span> &#125;,</span><br><span class="line">];</span><br><span class="line">minBy(people, <span class="string">&#x27;age&#x27;</span>);   <span class="comment">// &#123; name: &#x27;Bar&#x27;, age: 24 &#125;</span></span><br></pre></td></tr></table></figure>

<h2 id="展平一个数组"><a href="#展平一个数组" class="headerlink" title="展平一个数组"></a>展平一个数组</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> flat = <span class="function"><span class="params">arr</span> =&gt;</span> [].concat.apply([], arr.map(<span class="function"><span class="params">a</span> =&gt;</span> <span class="built_in">Array</span>.isArray(a) ? flat(a) : a));</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> flat = <span class="function"><span class="params">arr</span> =&gt;</span> arr.reduce(<span class="function">(<span class="params">a, b</span>) =&gt;</span> <span class="built_in">Array</span>.isArray(b) ? [...a, ...flat(b)] : [...a, b], []);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="comment">// See the browser compatibility at https://caniuse.com/#feat=array-flat\</span></span><br><span class="line"><span class="keyword">const</span> flat = <span class="function"><span class="params">arr</span> =&gt;</span> arr.flat();</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">flat([<span class="string">&#x27;cat&#x27;</span>, [<span class="string">&#x27;lion&#x27;</span>, <span class="string">&#x27;tiger&#x27;</span>]]);   <span class="comment">// [&#x27;cat&#x27;, &#x27;lion&#x27;, &#x27;tiger&#x27;]</span></span><br></pre></td></tr></table></figure>

<h2 id="获取所有连续元素的数组"><a href="#获取所有连续元素的数组" class="headerlink" title="获取所有连续元素的数组"></a>获取所有连续元素的数组</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> getConsecutiveArrays = <span class="function">(<span class="params">arr, size</span>) =&gt;</span> size &gt; arr.length ? [] : arr.slice(size - <span class="number">1</span>).map(<span class="function">(<span class="params">_, i</span>) =&gt;</span> arr.slice(i, size + i));</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">getConsecutiveArrays([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>], <span class="number">2</span>);   <span class="comment">// [[1, 2], [2, 3], [3, 4], [4, 5]]</span></span><br><span class="line">getConsecutiveArrays([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>], <span class="number">3</span>);   <span class="comment">// [[1, 2, 3], [2, 3, 4], [3, 4, 5]]</span></span><br><span class="line">getConsecutiveArrays([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>], <span class="number">6</span>);   <span class="comment">// []</span></span><br></pre></td></tr></table></figure>

<h2 id="获取数组的所有第-n-项"><a href="#获取数组的所有第-n-项" class="headerlink" title="获取数组的所有第 n 项"></a>获取数组的所有第 n 项</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> getNthItems = <span class="function">(<span class="params">arr, nth</span>) =&gt;</span> arr.filter(<span class="function">(<span class="params">_, i</span>) =&gt;</span> i % nth === nth - <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">getNthItems([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>], <span class="number">2</span>);    <span class="comment">// [2, 4, 6, 8]</span></span><br><span class="line">getNthItems([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>], <span class="number">3</span>);    <span class="comment">// [3, 6, 9]</span></span><br></pre></td></tr></table></figure>

<h2 id="获取数组的所有子集"><a href="#获取数组的所有子集" class="headerlink" title="获取数组的所有子集"></a>获取数组的所有子集</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> getSubsets = <span class="function"><span class="params">arr</span> =&gt;</span> arr.reduce(<span class="function">(<span class="params">prev, curr</span>) =&gt;</span> prev.concat(prev.map(<span class="function"><span class="params">k</span> =&gt;</span> k.concat(curr))), [[]]);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">getSubsets([<span class="number">1</span>, <span class="number">2</span>]);     <span class="comment">// [[], [1], [2], [1, 2]]</span></span><br><span class="line">getSubsets([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]);  <span class="comment">// [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]</span></span><br></pre></td></tr></table></figure>

<h2 id="获取数组中某个值的索引"><a href="#获取数组中某个值的索引" class="headerlink" title="获取数组中某个值的索引"></a>获取数组中某个值的索引</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> indices = <span class="function">(<span class="params">arr, value</span>) =&gt;</span> arr.reduce(<span class="function">(<span class="params">acc, v, i</span>) =&gt;</span> (v === value ? [...acc, i] : acc), []);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> indices = <span class="function">(<span class="params">arr, value</span>) =&gt;</span> arr.map(<span class="function">(<span class="params">v, i</span>) =&gt;</span> v === value ? i : <span class="literal">false</span>).filter(<span class="built_in">Boolean</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">indices([<span class="string">&#x27;h&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;l&#x27;</span>, <span class="string">&#x27;l&#x27;</span>, <span class="string">&#x27;o&#x27;</span>], <span class="string">&#x27;l&#x27;</span>);    <span class="comment">// [2, 3]</span></span><br><span class="line">indices([<span class="string">&#x27;h&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;l&#x27;</span>, <span class="string">&#x27;l&#x27;</span>, <span class="string">&#x27;o&#x27;</span>], <span class="string">&#x27;w&#x27;</span>);    <span class="comment">// []</span></span><br></pre></td></tr></table></figure>

<h2 id="获取数组的平均值"><a href="#获取数组的平均值" class="headerlink" title="获取数组的平均值"></a>获取数组的平均值</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> average = <span class="function"><span class="params">arr</span> =&gt;</span> arr.reduce(<span class="function">(<span class="params">a, b</span>) =&gt;</span> a + b, <span class="number">0</span>) / arr.length;</span><br></pre></td></tr></table></figure>

<h2 id="获取数组的交集"><a href="#获取数组的交集" class="headerlink" title="获取数组的交集"></a>获取数组的交集</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> getIntersection = <span class="function">(<span class="params">a, ...arr</span>) =&gt;</span> [...new <span class="built_in">Set</span>(a)].filter(<span class="function"><span class="params">v</span> =&gt;</span> arr.every(<span class="function"><span class="params">b</span> =&gt;</span> b.includes(v)));</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">getIntersection([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], [<span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]);               <span class="comment">// [2, 3]</span></span><br><span class="line">getIntersection([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], [<span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>], [<span class="number">1</span>, <span class="number">3</span>, <span class="number">5</span>]);    <span class="comment">// [3]</span></span><br></pre></td></tr></table></figure>

<h2 id="获取数字数组的排名"><a href="#获取数字数组的排名" class="headerlink" title="获取数字数组的排名"></a>获取数字数组的排名</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> ranking = <span class="function">(<span class="params">arr</span>) =&gt;</span> arr.map(<span class="function">(<span class="params">x, y, z</span>) =&gt;</span> z.filter(<span class="function"><span class="params">w</span> =&gt;</span> w &gt; x).length + <span class="number">1</span>);</span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">ranking([<span class="number">80</span>, <span class="number">65</span>, <span class="number">90</span>, <span class="number">50</span>]);      <span class="comment">// [2, 3, 1, 4]</span></span><br><span class="line">ranking([<span class="number">80</span>, <span class="number">80</span>, <span class="number">70</span>, <span class="number">50</span>]);      <span class="comment">// [1, 1, 3, 4]</span></span><br><span class="line">ranking([<span class="number">80</span>, <span class="number">80</span>, <span class="number">80</span>, <span class="number">50</span>]);      <span class="comment">// [1, 1, 1, 4]</span></span><br></pre></td></tr></table></figure>

<h2 id="获取一组数字的总和"><a href="#获取一组数字的总和" class="headerlink" title="获取一组数字的总和"></a>获取一组数字的总和</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> sum = <span class="function"><span class="params">arr</span> =&gt;</span> arr.reduce(<span class="function">(<span class="params">a, b</span>) =&gt;</span> a + b, <span class="number">0</span>);</span><br></pre></td></tr></table></figure>

<h2 id="获取数组的唯一值"><a href="#获取数组的唯一值" class="headerlink" title="获取数组的唯一值"></a>获取数组的唯一值</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> unique = <span class="function"><span class="params">arr</span> =&gt;</span> [...new <span class="built_in">Set</span>(arr)];</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> unique = <span class="function"><span class="params">arr</span> =&gt;</span> arr.filter(<span class="function">(<span class="params">el, i, array</span>) =&gt;</span> array.indexOf(el) === i);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> unique = <span class="function"><span class="params">arr</span> =&gt;</span> arr.reduce(<span class="function">(<span class="params">acc, el</span>) =&gt;</span> acc.includes(el) ? acc : [...acc, el], []);</span><br></pre></td></tr></table></figure>

<h2 id="获取数组的并集"><a href="#获取数组的并集" class="headerlink" title="获取数组的并集"></a>获取数组的并集</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> union = <span class="function">(<span class="params">...arr</span>) =&gt;</span> [...new <span class="built_in">Set</span>(arr.flat())];</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">union([<span class="number">1</span>, <span class="number">2</span>], [<span class="number">2</span>, <span class="number">3</span>], [<span class="number">3</span>]);     <span class="comment">// [1, 2, 3]</span></span><br></pre></td></tr></table></figure>

<h2 id="按键对一组对象进行分组"><a href="#按键对一组对象进行分组" class="headerlink" title="按键对一组对象进行分组"></a>按键对一组对象进行分组</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> groupBy = <span class="function">(<span class="params">arr, key</span>) =&gt;</span> arr.reduce(<span class="function">(<span class="params">acc, item</span>) =&gt;</span> ((acc[item[key]] = [...(acc[item[key]] || []), item]), acc), &#123;&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">groupBy([</span><br><span class="line"> &#123; <span class="attr">branch</span>: <span class="string">&#x27;audi&#x27;</span>, <span class="attr">model</span>: <span class="string">&#x27;q8&#x27;</span>, <span class="attr">year</span>: <span class="string">&#x27;2019&#x27;</span> &#125;,</span><br><span class="line"> &#123; <span class="attr">branch</span>: <span class="string">&#x27;audi&#x27;</span>, <span class="attr">model</span>: <span class="string">&#x27;rs7&#x27;</span>, <span class="attr">year</span>: <span class="string">&#x27;2020&#x27;</span> &#125;,</span><br><span class="line"> &#123; <span class="attr">branch</span>: <span class="string">&#x27;ford&#x27;</span>, <span class="attr">model</span>: <span class="string">&#x27;mustang&#x27;</span>, <span class="attr">year</span>: <span class="string">&#x27;2019&#x27;</span> &#125;,</span><br><span class="line"> &#123; <span class="attr">branch</span>: <span class="string">&#x27;ford&#x27;</span>, <span class="attr">model</span>: <span class="string">&#x27;explorer&#x27;</span>, <span class="attr">year</span>: <span class="string">&#x27;2020&#x27;</span> &#125;,</span><br><span class="line"> &#123; <span class="attr">branch</span>: <span class="string">&#x27;bmw&#x27;</span>, <span class="attr">model</span>: <span class="string">&#x27;x7&#x27;</span>, <span class="attr">year</span>: <span class="string">&#x27;2020&#x27;</span> &#125;,</span><br><span class="line">], <span class="string">&#x27;branch&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">&#123;</span></span><br><span class="line"><span class="comment"> audi: [</span></span><br><span class="line"><span class="comment">  &#123; branch: &#x27;audi&#x27;, model: &#x27;q8&#x27;, year: &#x27;2019&#x27; &#125;,</span></span><br><span class="line"><span class="comment">  &#123; branch: &#x27;audi&#x27;, model: &#x27;rs7&#x27;, year: &#x27;2020&#x27; &#125;</span></span><br><span class="line"><span class="comment"> ],</span></span><br><span class="line"><span class="comment"> bmw: [</span></span><br><span class="line"><span class="comment">  &#123; branch: &#x27;bmw&#x27;, model: &#x27;x7&#x27;, year: &#x27;2020&#x27; &#125;</span></span><br><span class="line"><span class="comment"> ],</span></span><br><span class="line"><span class="comment"> ford: [</span></span><br><span class="line"><span class="comment">  &#123; branch: &#x27;ford&#x27;, model: &#x27;mustang&#x27;, year: &#x27;2019&#x27; &#125;,</span></span><br><span class="line"><span class="comment">  &#123; branch: &#x27;ford&#x27;, model: &#x27;explorer&#x27;, year: &#x27;2020&#x27; &#125;</span></span><br><span class="line"><span class="comment"> ],</span></span><br><span class="line"><span class="comment">&#125;</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

<h2 id="合并两个数组"><a href="#合并两个数组" class="headerlink" title="合并两个数组"></a>合并两个数组</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Merge but don&#x27;t remove the duplications</span></span><br><span class="line"><span class="keyword">const</span> merge = <span class="function">(<span class="params">a, b</span>) =&gt;</span> a.concat(b);</span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> merge = <span class="function">(<span class="params">a, b</span>) =&gt;</span> [...a, ...b];</span><br><span class="line"></span><br><span class="line"><span class="comment">// Merge and remove the duplications</span></span><br><span class="line"><span class="keyword">const</span> merge = [...new <span class="built_in">Set</span>(a.concat(b))];</span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> merge = [...new <span class="built_in">Set</span>([...a, ...b])];</span><br></pre></td></tr></table></figure>

<h2 id="根据条件对数组进行分区"><a href="#根据条件对数组进行分区" class="headerlink" title="根据条件对数组进行分区"></a>根据条件对数组进行分区</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> partition = <span class="function">(<span class="params">arr, criteria</span>) =&gt;</span> arr.reduce(<span class="function">(<span class="params">acc, i</span>) =&gt;</span> (acc[criteria(i) ? <span class="number">0</span> : <span class="number">1</span>].push(i), acc), [[], []]);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">partition([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>], <span class="function"><span class="params">n</span> =&gt;</span> n % <span class="number">2</span>);     <span class="comment">// [[2, 4], [1, 3, 5]]</span></span><br></pre></td></tr></table></figure>

<h2 id="删除数组中的重复值"><a href="#删除数组中的重复值" class="headerlink" title="删除数组中的重复值"></a>删除数组中的重复值</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> removeDuplicate = <span class="function"><span class="params">arr</span> =&gt;</span> arr.filter(<span class="function"><span class="params">i</span> =&gt;</span> arr.indexOf(i) === arr.lastIndexOf(i));</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">removeDuplicate([<span class="string">&#x27;h&#x27;</span>, <span class="string">&#x27;e&#x27;</span>, <span class="string">&#x27;l&#x27;</span>, <span class="string">&#x27;l&#x27;</span>, <span class="string">&#x27;o&#x27;</span>, <span class="string">&#x27;w&#x27;</span>, <span class="string">&#x27;o&#x27;</span>, <span class="string">&#x27;r&#x27;</span>, <span class="string">&#x27;l&#x27;</span>, <span class="string">&#x27;d&#x27;</span>]); <span class="comment">//  [&#x27;h&#x27;, &#x27;e&#x27;, &#x27;w&#x27;, &#x27;r&#x27;, &#x27;d&#x27;]</span></span><br></pre></td></tr></table></figure>

<h2 id="从数组中删除假值"><a href="#从数组中删除假值" class="headerlink" title="从数组中删除假值"></a>从数组中删除假值</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> removeFalsy = <span class="function"><span class="params">arr</span> =&gt;</span> arr.filter(<span class="built_in">Boolean</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">removeFalsy([<span class="number">0</span>, <span class="string">&#x27;a string&#x27;</span>, <span class="string">&#x27;&#x27;</span>, <span class="literal">NaN</span>, <span class="literal">true</span>, <span class="number">5</span>, <span class="literal">undefined</span>, <span class="string">&#x27;another string&#x27;</span>, <span class="literal">false</span>]); <span class="comment">// [&#x27;a string&#x27;, true, 5, &#x27;another string&#x27;]</span></span><br></pre></td></tr></table></figure>

<h2 id="重复数组"><a href="#重复数组" class="headerlink" title="重复数组"></a>重复数组</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// `arr` is an array</span></span><br><span class="line"><span class="keyword">const</span> repeat = <span class="function">(<span class="params">arr, n</span>) =&gt;</span> [].concat(...Array(n).fill(arr));<span class="comment">// Orconst repeat = (arr, n) =&gt; Array(n).fill(arr).flat();</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> repeat = <span class="function">(<span class="params">arr, n</span>) =&gt;</span> <span class="built_in">Array</span>(arr.length * n).fill(<span class="number">0</span>).map(<span class="function">(<span class="params">_, i</span>) =&gt;</span> arr[i % arr.length]);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> repeat = <span class="function">(<span class="params">arr, n</span>) =&gt;</span> <span class="built_in">Array</span>.from(&#123; <span class="attr">length</span>: arr.length * n &#125;, <span class="function">(<span class="params">_, i</span>) =&gt;</span> arr[i % arr.length]);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">repeat([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], <span class="number">3</span>);       <span class="comment">// [1, 2, 3, 1, 2, 3, 1, 2, 3]</span></span><br></pre></td></tr></table></figure>

<h2 id="打乱数组"><a href="#打乱数组" class="headerlink" title="打乱数组"></a>打乱数组</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> shuffle = <span class="function"><span class="params">arr</span> =&gt;</span> arr.map(<span class="function"><span class="params">a</span> =&gt;</span> (&#123; <span class="attr">sort</span>: <span class="built_in">Math</span>.random(), <span class="attr">value</span>: a &#125;)).sort(<span class="function">(<span class="params">a, b</span>) =&gt;</span> a.sort - b.sort).map(<span class="function"><span class="params">a</span> =&gt;</span> a.value);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> shuffle = <span class="function"><span class="params">arr</span> =&gt;</span> arr.sort(<span class="function">() =&gt;</span> <span class="number">.5</span> - <span class="built_in">Math</span>.random());</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">shuffle([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">10</span>]);   <span class="comment">// [9, 1, 10, 6, 8, 5, 2, 3, 7, 4]</span></span><br></pre></td></tr></table></figure>

<h2 id="按给定键对项目数组进行排序"><a href="#按给定键对项目数组进行排序" class="headerlink" title="按给定键对项目数组进行排序"></a>按给定键对项目数组进行排序</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> sortBy = <span class="function">(<span class="params">arr, k</span>) =&gt;</span> arr.concat().sort(<span class="function">(<span class="params">a, b</span>) =&gt;</span> (a[k] &gt; b[k]) ? <span class="number">1</span> : ((a[k] &lt; b[k]) ? -<span class="number">1</span> : <span class="number">0</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line"><span class="keyword">const</span> people = [</span><br><span class="line"> &#123; <span class="attr">name</span>: <span class="string">&#x27;Foo&#x27;</span>, <span class="attr">age</span>: <span class="number">42</span> &#125;,</span><br><span class="line"> &#123; <span class="attr">name</span>: <span class="string">&#x27;Bar&#x27;</span>, <span class="attr">age</span>: <span class="number">24</span> &#125;,</span><br><span class="line"> &#123; <span class="attr">name</span>: <span class="string">&#x27;Fuzz&#x27;</span>, <span class="attr">age</span>: <span class="number">36</span> &#125;,</span><br><span class="line"> &#123; <span class="attr">name</span>: <span class="string">&#x27;Baz&#x27;</span>, <span class="attr">age</span>: <span class="number">32</span> &#125;,</span><br><span class="line">];</span><br><span class="line">sortBy(people, <span class="string">&#x27;age&#x27;</span>);</span><br><span class="line"><span class="comment">// returns</span></span><br><span class="line"><span class="comment">//  [</span></span><br><span class="line"><span class="comment">//      &#123; name: &#x27;Bar&#x27;, age: 24 &#125;,</span></span><br><span class="line"><span class="comment">//      &#123; name: &#x27;Baz&#x27;, age: 32 &#125;,</span></span><br><span class="line"><span class="comment">//      &#123; name: &#x27;Fuzz&#x27;, age: 36 &#125;,</span></span><br><span class="line"><span class="comment">//      &#123; name: &#x27;Foo&#x27;, age: 42 &#125;,</span></span><br><span class="line"><span class="comment">//  ]</span></span><br></pre></td></tr></table></figure>

<h2 id="对数字数组进行排序"><a href="#对数字数组进行排序" class="headerlink" title="对数字数组进行排序"></a>对数字数组进行排序</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> sort = <span class="function"><span class="params">arr</span> =&gt;</span> arr.sort(<span class="function">(<span class="params">a, b</span>) =&gt;</span> a - b);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">sort([<span class="number">1</span>, <span class="number">5</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">3</span>]);      <span class="comment">// [1, 2, 3, 4, 5]</span></span><br></pre></td></tr></table></figure>

<h2 id="将数组拆分为块"><a href="#将数组拆分为块" class="headerlink" title="将数组拆分为块"></a>将数组拆分为块</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> chunk = <span class="function">(<span class="params">arr, size</span>) =&gt;</span> arr.reduce(<span class="function">(<span class="params">acc, e, i</span>) =&gt;</span> (i % size ? acc[acc.length - <span class="number">1</span>].push(e) : acc.push([e]), acc), []);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Examples</span></span><br><span class="line">chunk([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>], <span class="number">3</span>);     <span class="comment">// [[1, 2, 3], [4, 5, 6], [7, 8]]</span></span><br><span class="line">chunk([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>], <span class="number">4</span>);     <span class="comment">// [[1, 2, 3, 4], [5, 6, 7, 8]]</span></span><br></pre></td></tr></table></figure>

<h2 id="交换矩阵的行和列"><a href="#交换矩阵的行和列" class="headerlink" title="交换矩阵的行和列"></a>交换矩阵的行和列</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> transpose = <span class="function"><span class="params">matrix</span> =&gt;</span> matrix[<span class="number">0</span>].map(<span class="function">(<span class="params">col, i</span>) =&gt;</span> matrix.map(<span class="function"><span class="params">row</span> =&gt;</span> row[i]));</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> transpose = <span class="function"><span class="params">matrix</span> =&gt;</span> matrix[<span class="number">0</span>].map(<span class="function">(<span class="params">col, c</span>) =&gt;</span> matrix.map(<span class="function">(<span class="params">row, r</span>) =&gt;</span> matrix[r][c]));</span><br><span class="line"></span><br><span class="line"><span class="comment">// Or</span></span><br><span class="line"><span class="keyword">const</span> transpose = <span class="function"><span class="params">matrix</span> =&gt;</span> matrix.reduce(<span class="function">(<span class="params">prev, next</span>) =&gt;</span> next.map(<span class="function">(<span class="params">item, i</span>) =&gt;</span> (prev[i] || []).concat(next[i])), []);</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">transpose([             <span class="comment">// [</span></span><br><span class="line"> [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>],             <span class="comment">//      [1, 4, 7],</span></span><br><span class="line"> [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>],             <span class="comment">//      [2, 5, 8],</span></span><br><span class="line"> [<span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>],             <span class="comment">//      [3, 6, 9],</span></span><br><span class="line">]);                     <span class="comment">// ]</span></span><br></pre></td></tr></table></figure>

<h2 id="交换两个数组项"><a href="#交换两个数组项" class="headerlink" title="交换两个数组项"></a>交换两个数组项</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// `i` must be less than `j`</span></span><br><span class="line"><span class="keyword">const</span> swapItems = <span class="function">(<span class="params">a, i, j</span>) =&gt;</span> a[i] &amp;&amp; a[j] &amp;&amp; [...a.slice(<span class="number">0</span>, i), a[j], ...a.slice(i + <span class="number">1</span>, j), a[i], ...a.slice(j + <span class="number">1</span>)] || a;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">swapItems([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>], <span class="number">1</span>, <span class="number">4</span>);   <span class="comment">// [1, 5, 3, 4, 2] </span></span><br></pre></td></tr></table></figure>

<h2 id="解压缩数组数组"><a href="#解压缩数组数组" class="headerlink" title="解压缩数组数组"></a>解压缩数组数组</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> unzip = <span class="function"><span class="params">arr</span> =&gt;</span> arr.reduce(<span class="function">(<span class="params">acc, c</span>) =&gt;</span> (c.forEach(<span class="function">(<span class="params">v, i</span>) =&gt;</span> acc[i].push(v)), acc), <span class="built_in">Array</span>.from(&#123; <span class="attr">length</span>: <span class="built_in">Math</span>.max(...arr.map(<span class="function"><span class="params">a</span> =&gt;</span> a.length)) &#125;, <span class="function">(<span class="params">_</span>) =&gt;</span> []));</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">unzip([[<span class="string">&#x27;a&#x27;</span>, <span class="number">1</span>], [<span class="string">&#x27;b&#x27;</span>, <span class="number">2</span>], [<span class="string">&#x27;c&#x27;</span>, <span class="number">3</span>], [<span class="string">&#x27;d&#x27;</span>, <span class="number">4</span>], [<span class="string">&#x27;e&#x27;</span>, <span class="number">5</span>]]);  <span class="comment">// [[&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;, &#x27;d&#x27;, &#x27;e&#x27;], [1, 2, 3, 4, 5]]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> a     1</span></span><br><span class="line"><span class="comment">  b   2</span></span><br><span class="line"><span class="comment">   c 3</span></span><br><span class="line"><span class="comment">   d 4</span></span><br><span class="line"><span class="comment">   e 5</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

<h2 id="压缩多个数组"><a href="#压缩多个数组" class="headerlink" title="压缩多个数组"></a>压缩多个数组</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> zip = <span class="function">(<span class="params">...arr</span>) =&gt;</span> <span class="built_in">Array</span>.from(&#123; <span class="attr">length</span>: <span class="built_in">Math</span>.max(...arr.map(<span class="function"><span class="params">a</span> =&gt;</span> a.length)) &#125;, <span class="function">(<span class="params">_, i</span>) =&gt;</span> arr.map(<span class="function"><span class="params">a</span> =&gt;</span> a[i]));</span><br><span class="line"></span><br><span class="line"><span class="comment">// Example</span></span><br><span class="line">zip([<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;d&#x27;</span>, <span class="string">&#x27;e&#x27;</span>], [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]);   <span class="comment">// [[&#x27;a&#x27;, 1], [&#x27;b&#x27;, 2], [&#x27;c&#x27;, 3], [&#x27;d&#x27;, 4], [&#x27;e&#x27;, 5]]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> Does it look like a zipper?</span></span><br><span class="line"><span class="comment">     a 1</span></span><br><span class="line"><span class="comment">     b 2</span></span><br><span class="line"><span class="comment">    c   3</span></span><br><span class="line"><span class="comment">   d     4</span></span><br><span class="line"><span class="comment">  e       5</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://vixcity.gitee.io/my-gitee-blog">Vixcity</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://vixcity.gitee.io/my-gitee-blog/2021/09/23/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91%E6%95%B0%E7%BB%84%E7%AF%87/">http://vixcity.gitee.io/my-gitee-blog/2021/09/23/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91%E6%95%B0%E7%BB%84%E7%AF%87/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://vixcity.gitee.io/my-gitee-blog" target="_blank">Vixcity</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/my-gitee-blog/tags/%E4%BB%A3%E7%A0%81%E7%89%87%E6%AE%B5/">代码片段</a><a class="post-meta__tags" href="/my-gitee-blog/tags/JavaScript/">JavaScript</a><a class="post-meta__tags" href="/my-gitee-blog/tags/%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81/">一行代码</a></div><div class="post_share"><div class="social-share" data-image="/my-gitee-blog/img/JS.jpg" data-sites="wechat,qq"></div><link rel="stylesheet" href="/my-gitee-blog/" media="print" onload="this.media='all'"><script src="/my-gitee-blog/" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/my-gitee-blog/2021/09/23/%E3%80%90HTML%E3%80%91%E5%A6%82%E4%BD%95%E8%AE%BE%E7%BD%AEDIV%E5%8F%AF%E7%BC%96%E8%BE%91/"><img class="prev-cover" src="/my-gitee-blog/img/HTML.jpg" onerror="onerror=null;src='/my-gitee-blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">【HTML】如何设置DIV可编辑</div></div></a></div><div class="next-post pull-right"><a href="/my-gitee-blog/2021/09/22/%E3%80%90CSS%E3%80%91%E5%AE%9E%E7%8E%B0%E6%96%B0%E6%8B%9F%E6%80%81(Neumorphism)%20UI%20%E9%A3%8E%E6%A0%BC/"><img class="next-cover" src="/my-gitee-blog/img/CSS.jpg" onerror="onerror=null;src='/my-gitee-blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">【CSS】实现新拟态(Neumorphism) UI 风格</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/my-gitee-blog/2021/09/26/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91DOM%E8%8A%82%E7%82%B9%E7%AF%87/" title="【一行代码可以实现的事】DOM节点篇"><img class="cover" src="/my-gitee-blog/img/JS.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-26</div><div class="title">【一行代码可以实现的事】DOM节点篇</div></div></a></div><div><a href="/my-gitee-blog/2021/09/29/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91%E5%85%B6%E4%BB%96%E7%AF%87/" title="【一行代码可以实现的事】其他篇"><img class="cover" src="/my-gitee-blog/img/JS.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-29</div><div class="title">【一行代码可以实现的事】其他篇</div></div></a></div><div><a href="/my-gitee-blog/2021/09/28/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91%E5%87%BD%E6%95%B0%E7%AF%87/" title="【一行代码可以实现的事】函数篇"><img class="cover" src="/my-gitee-blog/img/JS.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-28</div><div class="title">【一行代码可以实现的事】函数篇</div></div></a></div><div><a href="/my-gitee-blog/2021/10/01/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%AF%87/" title="【一行代码可以实现的事】字符串篇"><img class="cover" src="/my-gitee-blog/img/JS.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-01</div><div class="title">【一行代码可以实现的事】字符串篇</div></div></a></div><div><a href="/my-gitee-blog/2021/09/30/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91%E6%95%B0%E5%AD%97%E7%AF%87/" title="【一行代码可以实现的事】数字篇"><img class="cover" src="/my-gitee-blog/img/JS.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-30</div><div class="title">【一行代码可以实现的事】数字篇</div></div></a></div><div><a href="/my-gitee-blog/2021/09/30/%E3%80%90%E4%B8%80%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%8F%AF%E4%BB%A5%E5%AE%9E%E7%8E%B0%E7%9A%84%E4%BA%8B%E3%80%91%E5%AF%B9%E8%B1%A1%E7%AF%87/" title="【一行代码可以实现的事】对象篇"><img class="cover" src="/my-gitee-blog/img/JS.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-30</div><div class="title">【一行代码可以实现的事】对象篇</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="lv-container" data-id="city" data-uid="MTAyMC81NDQ0NS8zMDkxNg=="></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/my-gitee-blog/img/avatar.png" onerror="this.onerror=null;this.src='/my-gitee-blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Vixcity</div><div class="author-info__description">今日事，今日毕</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/my-gitee-blog/archives/"><div class="headline">文章</div><div class="length-num">55</div></a></div><div class="card-info-data-item is-center"><a href="/my-gitee-blog/tags/"><div class="headline">标签</div><div class="length-num">50</div></a></div><div class="card-info-data-item is-center"><a href="/my-gitee-blog/categories/"><div class="headline">分类</div><div class="length-num">18</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/Vixcity"><i class="fab fa-github"></i><span>我的Github</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/Vixcity" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:2091283625@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">这是Vixcity目前的小博客</div></div><div class="sticky_layout"><div class="card-widget card-history"><div class="card-content"><div class="item-headline"><i class="fas fa-clock fa-spin"></i><span>那年今日</span></div><div id="history-baidu" style="height: 100px;overflow: hidden;"><div class="history_swiper-container" id="history-container" style="width: 100%;height: 100%;"><div class="swiper-wrapper" id="history_container_wrapper" style="height:20px"></div></div></div></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/13/%E3%80%90NodeJS%E3%80%91NodeJS%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%E8%AF%B7%E6%B1%82/" title="【NodeJS】NodeJS 服务端请求"><img src="/my-gitee-blog/img/NodeJs.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【NodeJS】NodeJS 服务端请求"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/13/%E3%80%90NodeJS%E3%80%91NodeJS%20%E6%9C%8D%E5%8A%A1%E7%AB%AF%E8%AF%B7%E6%B1%82/" title="【NodeJS】NodeJS 服务端请求">【NodeJS】NodeJS 服务端请求</a><time datetime="2022-07-12T16:00:00.000Z" title="发表于 2022-07-13 00:00:00">2022-07-13</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%201/" title="【Hexo】标签样式列表参考 - 1"><img src="/my-gitee-blog/img/hexo.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【Hexo】标签样式列表参考 - 1"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%201/" title="【Hexo】标签样式列表参考 - 1">【Hexo】标签样式列表参考 - 1</a><time datetime="2022-07-11T16:00:00.000Z" title="发表于 2022-07-12 00:00:00">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%202/" title="【Hexo】标签样式列表参考 - 2"><img src="/my-gitee-blog/img/hexo.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【Hexo】标签样式列表参考 - 2"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%202/" title="【Hexo】标签样式列表参考 - 2">【Hexo】标签样式列表参考 - 2</a><time datetime="2022-07-11T16:00:00.000Z" title="发表于 2022-07-12 00:00:00">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%203/" title="【Hexo】标签样式列表参考 - 3"><img src="/my-gitee-blog/img/hexo.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【Hexo】标签样式列表参考 - 3"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/12/%E3%80%90Hexo%E3%80%91%E6%A0%87%E7%AD%BE%E6%A0%B7%E5%BC%8F%E5%88%97%E8%A1%A8%E5%8F%82%E8%80%83%20-%203/" title="【Hexo】标签样式列表参考 - 3">【Hexo】标签样式列表参考 - 3</a><time datetime="2022-07-11T16:00:00.000Z" title="发表于 2022-07-12 00:00:00">2022-07-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/my-gitee-blog/2022/07/07/%E3%80%90NodeJS%E3%80%91NodeJS%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/" title="【NodeJS】NodeJS入门学习"><img src="/my-gitee-blog/img/NodeJs.jpg" onerror="this.onerror=null;this.src='/my-gitee-blog/img/404.jpg'" alt="【NodeJS】NodeJS入门学习"/></a><div class="content"><a class="title" href="/my-gitee-blog/2022/07/07/%E3%80%90NodeJS%E3%80%91NodeJS%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0/" title="【NodeJS】NodeJS入门学习">【NodeJS】NodeJS入门学习</a><time datetime="2022-07-06T16:00:00.000Z" title="发表于 2022-07-07 00:00:00">2022-07-07</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/my-gitee-blog/img/JS.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By Vixcity</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div id="running-time"></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/my-gitee-blog/js/utils.js"></script><script src="/my-gitee-blog/js/main.js"></script><script src="/my-gitee-blog/js/tw_cn.js"></script><script src="/my-gitee-blog/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>function loadLivere () {
  if (typeof LivereTower === 'object') {
    window.LivereTower.init()
  }
  else {
    (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
    })(document, 'script');
  }
}

if ('Livere' === 'Livere' || !false) {
  if (false) btf.loadComment(document.getElementById('lv-container'), loadLivere)
  else loadLivere()
}
else {
  function loadOtherComment () {
    loadLivere()
  }
}</script></div><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="/my-gitee-blog/js/newStyle.js"></script><script src="/my-gitee-blog/js/sakura.js"></script><script>setInterval(()=>{let create_time=Math.round(new Date("2021-9-8-00:00:00").getTime()/1000);let timestamp=Math.round((new Date().getTime()+8*60*60*1000)/1000);let second=timestamp-create_time;let time=new Array(0,0,0,0,0);if(second>=365*24*3600){time[0]=parseInt(second/(365*24*3600));second%=365*24*3600}if(second>=24*3600){time[1]=parseInt(second/(24*3600));second%=24*3600}if(second>=3600){time[2]=parseInt(second/3600);second%=3600}if(second>=60){time[3]=parseInt(second/60);second%=60}if(second>0){time[4]=second}currentTimeHtml='小博客已经安全运行 '+time[0]+' 年 '+time[1]+' 天 '+time[2]+' 时 '+time[3]+' 分 '+time[4]+' 秒';var elementById=document.getElementById('running-time');if(elementById){elementById.innerHTML=currentTimeHtml}},1000);</script><script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script><script src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-card-history/baiduhistory/js/main.js"></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-nest.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><script data-pjax>function electric_clock_injector_config(){
                var parent_div_git = document.getElementsByClassName('sticky_layout')[0];
                var item_html = '<div class="card-widget card-clock"><div class="card-glass"><div class="card-background"><div class="card-content"><div id="hexo_electric_clock"><img id="card-clock-loading" src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-clock/clock/images/weather/loading.gif" style="height: 120px; width: 100%;" data-ll-status="loading" class="entered loading"></div></div></div></div></div>';
                console.log('已挂载electric_clock')
                // parent_div_git.innerHTML=item_html+parent_div_git.innerHTML // 无报错，但不影响使用(支持pjax跳转)
                parent_div_git.insertAdjacentHTML("afterbegin",item_html) // 有报错，但不影响使用(支持pjax跳转)
            }if( document.getElementsByClassName('sticky_layout')[0] && (location.pathname ==='all'|| 'all' ==='all')){

            electric_clock_injector_config()
        } </script><script src="https://pv.sohu.com/cityjson?ie=utf-8"></script><script data-pjax  src="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.js"></script><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1s');
    arr[i].setAttribute('data-wow-delay', '0.5s');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer src="https://unpkg.zhimg.com/hexo-butterfly-wowjs/lib/wow.min.js"></script><script defer src="https://unpkg.zhimg.com/hexo-butterfly-wowjs/lib/wow_init.js"></script><!-- hexo injector body_end end --><script src="/my-gitee-blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body></html>